package hu.szyrt.bug.db;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import android.content.Context;

import hu.szyrt.bug.R;
import hu.szyrt.bug.models.PlaceItem;

public class DiscountHandler {

	
	private final static SimpleDateFormat voucherFormat = new SimpleDateFormat("HHmm");
	private PlaceItem place;

	public DiscountHandler(PlaceItem place) {
		this.place = place;
	}
	
	
	public boolean isInfinityDiscounts(){
		return place.getDiscountType() != 0 && place.getDiscountsNumber() == 0;	
	}
	
	public boolean hasDiscount(){
		return place.getDiscountsNumber() > 0 && place.getDiscountRemainingNumber() > 0;
	}
	
	public boolean isAvailableNow(){
		if(place.getDiscountStartDate() == null || place.getDiscountEndDate() == null) 
			return false;
		
		Date now = new Date();
		return place.getDiscountStartDate().getTime() < now.getTime() && 
		 	   place.getDiscountEndDate().getTime() > now.getTime();
	}
	
	public static String generateVoucherID(Context context){
		String[] voucherIDs = context.getResources().getStringArray(R.array.voucher_ids);
		Random rnd = new Random();
		String voucher = voucherIDs[rnd.nextInt(voucherIDs.length)];
		return voucher + voucherFormat.format(new Date());
	}
	
	// Getters ----------------------------
	public PlaceItem getPlace() {
		return place;
	}
	
}
